;(function () {
  Array.prototype.delArrCf = function() {
    var res = [this[0]];
    for (var i = 1; i < this.length; i++) {
      var repeat = false;
      for (var j = 0; j < res.length; j++) {
        if (this[i].projectId == res[j].projectId) {
          repeat = true;
        }
      }
      if (!repeat) {
        res.push(this[i]);
      }
    }
    return res;
  };

  new Vue({
    el: '#contract-edit1',
    data: {
      contractId:hlUrlUtil.GetQueryString('id'),
      zlfType: {
        list: [{name: '公司租赁', type: 'C'}, {name: '个人租赁', type: 'P'}],
        active: 'C'
      },
      contactType: {
        list: [{name: '整租合同', type: 'ER'}, {name: '工位合同', type: 'WR'}],
        active: ''
      },
      channelType:{
        list: [],
        selected: {'name':null,'code':null}
      },
      channelName:{
        list: [],
        selected: {'name':null,'code':null}
      },
      industryTypes: {
        list: [{memo: ''}],
        selected: {code:'', name:''}
      },
      

      customers: [],   // 客户信息
      customersFlag: false,
      zlfData: {
        zlf_name: null,     // 租赁方公司名称
        zlf_id: null,
        zlf_lperson: null,   //租赁方法人
        zlf_sperson: null,   //租赁方签订人
        zlf_lphone: null,   //租赁方法人手机
        zlf_sphone: null,   //租赁方签订人手机
        zlf_address: null,  //租赁方地址
        m_code: null,    //管理编号
        c_code: null,    //合同编号
        name: null,    //合同名
        memo:null,     //备注
        idcode:null,   //识别号
        email:null,//邮箱,
				shortName: null
      },
      projects: {
        list: [],             // 项目列表
        selectedProjectName: '',  // 选定的项目名
        buildingData: [],      // 楼宇数据
        floorsdata: [],        // 楼层数据
        unitdata: [],          // 单元数据
        stWpdata: [],          // 工位车位数据
        id: null,
        buildingId: null,
        floorId: null,
        unitId: null
      },
      unitData: [],   // 选定的单元
      wpData: [],     // 选定的工位
      stData: [],     // 选定的车位
      wpNum:0,        // 工位数
      stNum:0,        // 车位数
      // 弹出层控制
      bounced: {
        unitFlag: false,
        stFlag: false,
        wpFlag: false
      },
      projectInfo: {
        project_id: null,
        project_name: null,
        units: [],    // 单元
        stdata: [],   // 车位
        wpdata: [],   // 工位
        unitFlag: false,
        stFlag: false,
        wpFlag: false
      },


      getname: {
        buildname: null,
        unitname: null,
        floorname: null,
        stop_acreage: null,
        fname: null
      },
      intent: {
        id:null,
        intentDeductibleAmount:null
      }
    },
    filters: {
      formatNum:function(item){
        if(item === '')return
        return hlNumberUtil.formatNum(item)
      },
      ellipsis:function(item){
        if(item.length > 9)return item.substr(0,10) + '...';
        return item
      }
    },
    computed: {
      zlfTypess: function () {
        var resutl;
        var self = this;
        self.industryTypes.list.map(function (item) {
          if (item.dictCode == self.industryTypes.selected.code) {
            resutl = item.dictName;
          }
        })
        return resutl;
      }
    },
    
    methods: {
      getChannelType(){
        hlhttp.get({
          url:'configure/dict',
          data:{'module_code':'ZHSH','config_name':'ZS_CHANNEL_CATE'},
          success:res => {
            this.channelType.list = res;
          }
        })
      },
      selectChannelName(ops){
        this.channelName.selected = {'name':ops.name,'code':ops.code}
      },
      selectChannelType(code,name){
        this.channelType.selected = {'name':name,'code':code}
        this.channelName.selected = {'name':null,'code':null}
      },
      // 获取合同详情
      getContractDetail:function(){
        var self = this;
        let url = 'contract'
        let data = {id:self.contractId}
        if (hlUrlUtil.GetQueryString('relet') == 'true') {
          url = 'contract/renewal'
          data = {contract_id: self.contractId}
        }

        hlhttp.get({
          url: url,
          data: data,
          success: function (data) {
            self.channelType.selected = {'name':data.channelCategoryVal,'code':data.channelCategoryCode}
            self.channelName.selected = {'name':data.channelNameVal,'code':data.channelNameCode}
            // 合同信息
            self.projectInfo.project_id = data.projectId;
            self.projectInfo.project_name = data.projectName;
            self.zlfData.m_code= data.mCode;
            self.zlfData.c_code= data.cCode;
            self.zlfData.memo = data.memo;
            self.zlfData.name= data.name;
            // 意向合同信息
            if(data.relatedIntentId){
              self.intent.id = data.relatedIntentId
              self.intent.intentDeductibleAmount = data.intentDeductibleAmount
            }

            // 乙方信息
            self.zlfData.zlf_name = data.zlfCompany;
            self.zlfData.zlf_id = data.zlfId;
            self.zlfData.idcode = data.idcode;
            self.zlfData.email = data.email;
            self.zlfData.zlf_lperson = data.zlfLperson;
            self.zlfData.zlf_sperson = data.zlfSperson;
            self.zlfData.zlf_lphone= data.zlfLphone;
            self.zlfData.zlf_sphone= data.zlfSphone;
            self.zlfData.zlf_address= data.zlfAddress;
            self.zlfData.shortName= data.shortName;
            self.industryTypes.selected = {'code':data.zlfIndustryType,'name':data.zlfIndustryType};
            self.zlfType.active = data.zlfType;
            // 房源信息
            self.contactType.active = data.contractType;
            self.projects.id = data.projectId;
            self.projects.selectedProjectName = data.projectName;
            // 单元  整租合同才能够出租单元
            if(data.contractType === 'ER'){
              if(data.units.length !== 0){
                var unitsArr = [];
                data.units.map(function(_item){
                  if(_item.positions.length === 0){
                    var obj = {
                      'building_id':_item.buildingId,
                      'building_name':_item.buildingName,
                      'floor_id':_item.floorId,
                      'floor_num':_item.floorNum,
                      'floor_name':_item.floorName,
                      'unit_name':_item.unitName,
                      'unit_acreage':_item.unitAcreage,
                      'unit_id':_item.unitId
                    }
                    unitsArr.push(obj)
                  }
                })
                self.projectInfo.units = unitsArr;
                self.unitData = JSON.parse(JSON.stringify(unitsArr));
                self.projectInfo.unitFlag = true;
              }
            }
            // 车位
            if(data.units.length !== 0){
              var stArr = [];
              var stNum = 0;
              data.units.map(function(_item){
                if(_item.positions.length > 0 && _item.unitType === 'ST'){
                  var obj = {
                    'building_id':_item.buildingId,
                    'building_name':_item.buildingName,
                    'floor_id':_item.floorId,
                    'floor_num':_item.floorNum,
                    'floor_name':_item.floorName,
                    'unit_id':_item.unitId,
                    'unit_name':_item.unitName,
                    'unit_acreage':_item.unitAcreage,
                    'stop':_item.positions
                  }
                  stArr.push(obj)
                  stNum += _item.positions.length;
                }
              })
              if(stArr.length !== 0){
                self.projectInfo.stFlag = true;
                self.projectInfo.stdata = stArr;
                self.stData = JSON.parse(JSON.stringify(stArr));
                self.stNum = stNum;
              }
            }
            // 工位
            if(data.units.length !== 0){
                var wpArr = [];
                var wpNum = 0;
                data.units.map(function(_item){
                  if(_item.positions.length > 0 && _item.unitType === 'WP'){
                    var obj = {
                      'building_id':_item.buildingId,
                      'building_name':_item.buildingName,
                      'floor_id':_item.floorId,
                      'floor_num':_item.floorNum,
                      'floor_name':_item.floorName,
                      'unit_id':_item.unitId,
                      'unit_name':_item.unitName,
                      'unit_acreage':_item.unitAcreage,
                      'stop':_item.positions
                    }
                    wpArr.push(obj)
                    wpNum += _item.positions.length;
                  }
                  if(wpArr.length !== 0){
                    self.projectInfo.wpFlag = true;
                    self.projectInfo.wpdata = wpArr;
                    self.wpData = JSON.parse(JSON.stringify(wpArr));
                    self.wpNum = wpNum;
                  }
                })
              }

          }
        })
      },
      //选择租赁房类型
      selectZlfType: function (type) {
        this.zlfType.active = type;
      },
      //获取行业列表
      getIndustryTypes: function () {
        var self = this;
        hlhttp.get({
          url: 'configure/industry/dict',
          success: function (data) {
            self.industryTypes.list = data;
          }
        })
      },
      //设置行业类型
      setIndustryType: function (code, name) {
        this.industryTypes.selected = {'code':code,'name':name}
      },
      //获取关联客户
      getRelevanceCustomer: function () {
        // 没有招商模块的权限时  没有关联客户
        var self = this;
        var permissions = JSON.parse(window.localStorage.getItem('permissions'));
        if(permissions.indexOf('c43') == -1){ return false;}

        var fetchCustomersMes = function(keyWord){
          hlhttp.get({
            url: 'customers/_search',
            data: {
              begin_date: '#',
              end_date: '#',
              key_word: keyWord,
              customer_state: '#',
              black_state: '#',
              page: 1,
              page_size: 10000,
              rperson_ids: '#',
              columns: '#',
              orders: '#'
            },
            success: function (data) {
              var temp = data.customers;

              if (temp.length === 0) {
                self.zlfData.zlf_id  = '#'
                return false;
              }
              self.customers = temp;
              self.customersFlag = true;

              temp.map(function(_el){
                if(_el.name === self.zlfData.zlf_name){
                  self.industryTypes.selected.code = _el.industryCode;
                  self.zlfData.zlf_id = _el.id;
                }
              })

              self.industryTypes.list.map(function (item) {
                if (item.dictCode == self.industryTypes.selected.code) {
                  self.industryTypes.selected.name = item.dictName;
                }
              })
            }
          })
        }
        var name = self.zlfData.zlf_name;

        self.zlf_id = null;
        self.industryTypes.selected.code = null ;
        self.industryTypes.selected.name = null ;
        $('#industry-st span').text('请选择行业')
        if(!name || hlNumberUtil.isNull(name)){
          self.customersFlag = false;
          return false;
        }
        fetchCustomersMes(name)
      },
      // 获取选中的客户所有关联信息
      selectedCustomerName: function (name) {
        var self = this;
        var customersArr = self.customers;

        self.zlfData.zlf_name = name;
        customersArr.map(function(_el){
          if(_el.name === name){
            self.zlfData['zlf_id'] = _el.id;
            self.zlfData['zlf_lperson'] = _el.legalPersonName;
            self.zlfData['zlf_sperson'] = _el.lperson;
            self.zlfData['zlf_sphone'] = _el.phone;
            self.zlfData['idcode'] = _el.idcode;
            self.zlfData['email'] = _el.email;
            self.zlfData['shortName'] = _el.shortName;
            self.zlf_id = _el.id;
            var industry = _el.industryCode;

            self.industryTypes.selected.code = _el.industryCode;
            self.$refs.channelType.innerHTML = _el.channelCategoryVal;
            self.channelType.selected.code = _el.channelCategoryCode;
            self.channelType.selected.code = _el.channelNameCode;
            self.channelName.selected.name = _el.channelNameVal;
          }

          self.industryTypes.list.map(function (item) {
            if (item.dictCode == self.industryTypes.selected.code) {
              self.industryTypes.selected.name = item.dictName;
            }
          })

          $('#industry-st span').text(self.industryTypes.selected.name)
        })

        self.customersFlag = false;
      },
      //设置合同类型
      setContactType: function (type) {
        this.contactType.active = type;
        // 整租合同  单元、车位       工位合同   工位、车位
        this.wpData = [];
        this.unitData = [];
        this.stData = [];
        this.projectInfo.wpdata = [];
        this.projectInfo.units = [];
        this.projectInfo.stdata = [];
      },
      //获取签约项目
      getProjects: function () {
        var self = this;
        hlhttp.get({
          url: 'user/projects',
          success: function (data) {
            self.projects.list = data.delArrCf();
          }
        })
      },
      //获取项目id
      getProjectMes: function (id, name, contractNoPrefix , contractNamePrefix) {
        this.projects.id = id;
        this.projectInfo.project_id = id;
        this.projectInfo.project_name = name;
        this.wpData = [];
        this.unitData = [];
        this.stData = [];
        this.projectInfo.wpdata = [];
        this.projectInfo.units = [];
        this.projectInfo.stdata = [];

				if(contractNoPrefix){ this.zlfData.c_code = contractNoPrefix + (this.zlfData.c_code || '') }
				if(contractNamePrefix){ this.zlfData.name = contractNamePrefix + (this.zlfData.name || '') }

      },
      //获取楼宇id:
      getBuildId: function (id, name) {
        this.projects.buildingId = id;
        this.getname.buildname = name;
        this.projects.stWpdata = [];
        $('.floor-select span, .unit-select span, .building-select span').text('请选择')
        var _this = this;
        hlhttp.get({
          url: 'building/floors',
          data: {
            project_id: _this.projects.id,
            building_id: id,
          },
          success: function (res) {
            _this.projects.unitdata = [];
            if (res.length === 0) {
              hl.toast('暂无楼层，请重新选择')
              return false
            }
            _this.projects.floorsdata = res
          }
        })
      },
      //获取楼宇
      getBuildings: function (n) {
        var self = this;
        if (!self.projects.id){
          hl.toast('请先选择项目')
          return false;
        }
        // n == 1 单元弹出层   n == 2 工位弹出层   n == 3 车位弹出层
        if (n === 1) self.bounced.unitFlag = true;
        if (n === 2) self.bounced.wpFlag = true;
        if (n === 3) self.bounced.stFlag = true;
        $('.unit-select span').text('请选择')

        hlhttp.get({
          url: 'project/buildings',
          data: {project_id: self.projects.id},
          success: function (res) {
            self.projects.buildingData = res
          }
        })
      },
      //获取单元（所有单元）
      getUnit: function (id, fname) {
        this.projects.floorId = id;
        this.getname.fname = fname;
        var self = this;
        this.projects.unitdata = [];
        $('.last-unit span').removeClass('activeNot active')
        $('.unit-select span').text('请选择')

        hlhttp.get({
          url: 'floor/units',
          data: {project_id: self.projects.id,floor_id: self.projects.floorId,building_id: self.projects.buildingId},
          success: function (res) {
            if (res.length === 0) {
              hl.toast('暂无单元，请重新选择')
              return false
            }
            self.projects.unitdata = res;
            setTimeout(function() {
              self.selectUnit()
            }, 200);
          }
        })
      },
      //查询
      queryUnit: function () {
        if (!this.projects.floorId) return;
        var _this = this;
        hlhttp.get({
          url: 'floor/units',
          data: {
            project_id: _this.projects.id,
            floor_id: _this.projects.floorId,
            building_id: _this.projects.buildingId
          },
          success: function (res) {
            if (res.length === 0) {
              hl.toast('暂无单元，请重新选择')
              return false
            }
            _this.projects.unitdata = res
          }
        })
      },
      //选中取消单元
      selUnit: function (e, i, id) {
        var data = this.projects.unitdata;
        if ($(e.srcElement).hasClass('activeNot')) {
          hl.toast('无法操作选择过的单元')
          return
        }

        if (!$(e.srcElement).hasClass('active')) {
          $(e.srcElement).addClass('active')
          this.projectInfo.units.push({
            building_id: data[i].buildingId,
            building_name: this.getname.buildname,
            floor_id: data[i].floorId,
            floor_num: data[i].floor,
            floor_name: this.getname.fname,
            unit_id: data[i].unitId,
            unit_name: data[i].unitName,
            unit_acreage: data[i].acreage
          })
        } else {
          $(e.srcElement).removeClass('active')
          for (var j = 0; j < this.projectInfo.units.length; j++) {
            if (this.projectInfo.units[j].unit_id == id) {
              this.projectInfo.units.splice(j, 1)
            }
          }
        }
      },
      //选中取消车位
      selUnitSt: function (e, i, id, name) {
        var data = this.projects.stWpdata;
        if ($(e.srcElement).hasClass('activeNot')) {
          hl.toast('无法操作选择过的单元')
          return
        }

        if (!$(e.srcElement).hasClass('active')) {
          $(e.srcElement).addClass('active')
          if (!this.projectInfo.stdata.length) {
            this.projectInfo.stdata.push({
              building_id: this.projects.buildingId,
              building_name: this.getname.buildname,
              floor_id: this.projects.floorId,
              floor_num: this.getname.floorname,
              floor_name: this.getname.fname,
              unit_id: this.projects.unitId,
              unit_name: this.getname.unitname,
              stop: [{positionId: id, positionName: name}],   // 车位
              unit_acreage: data[i].acreage
            })
          } else {
            var flag = false; index = null;
            for (var j = 0; j < this.projectInfo.stdata.length; j++) {
              if (this.projectInfo.stdata[j].unit_id == this.projects.unitId) {
                index = j;
                flag = 1;
              } else {
                index = j;
                flag = 2;
              }
            }

            if (flag == 1) {
              this.projectInfo.stdata[index].stop.push({positionId: id, positionName: name})
            }
            if (flag == 2) {
              this.projectInfo.stdata.push({
                building_id: this.projects.buildingId,
                building_name: this.getname.buildname,
                floor_id: this.projects.floorId,
                floor_num: this.getname.floorname,
                floor_name: this.getname.fname,
                unit_id: this.projects.unitId,
                unit_name: this.getname.unitname,
                stop: [{positionId: id, positionName: name}],         // 车位
                unit_acreage: data[i].acreage
              })
            }
          }
        } else {
          $(e.srcElement).removeClass('active')
          for (var j = 0; j < this.projectInfo.stdata.length; j++) {
            if (this.projectInfo.stdata[j].positionId == id) {
              this.projectInfo.stdata.splice(j, 1)
            }
          }
        }
      },
      //选中取消工位
      selUnitWp: function (e, i, id, name) {
        var data = this.projects.stWpdata;
        if ($(e.srcElement).hasClass('activeNot')) {
          hl.toast('无法操作选择过的单元')
          return
        }

        if (!$(e.srcElement).hasClass('active')) {
          $(e.srcElement).addClass('active')
          if (!this.projectInfo.wpdata.length) {
            this.projectInfo.wpdata.push({
              building_id: this.projects.buildingId,
              building_name: this.getname.buildname,
              floor_id: this.projects.floorId,
              floor_num: this.getname.floorname,
              floor_name: this.getname.fname,
              unit_id: this.projects.unitId,
              unit_name: this.getname.unitname,
              stop: [{positionId: id,positionName: name}],
              unit_acreage: data[i].acreage ? data[i].acreage : ''
            })
          } else {
            var flag = false; index = null;
            for (var j = 0; j < this.projectInfo.wpdata.length; j++) {
              if (this.projectInfo.wpdata[j].unit_id == this.projects.unitId) {
                index = j;
                flag = 1;
              } else {
                index = j;
                flag = 2;
              }
            }
            if (flag == 1) {
              this.projectInfo.wpdata[index].stop.push({positionId: id, positionName: name})
            }
            if (flag == 2) {
              this.projectInfo.wpdata.push({
                building_id: this.projects.buildingId,
                building_name: this.getname.buildname,
                floor_id: this.projects.floorId,
                floor_num: this.getname.floorname,
                floor_name: this.getname.fname,
                unit_id: this.projects.unitId,
                unit_name: this.getname.unitname,
                stop: [{positionId: id, positionName: name}],
                unit_acreage: data[i].acreage
              })
            }
          }
        } else {
          $(e.srcElement).removeClass('active')
          for (var j = 0; j < this.projectInfo.wpdata.length; j++) {
            if (this.projectInfo.wpdata[j].positionId == id) {
              this.projectInfo.wpdata.splice(j, 1)
            }
          }
        }
      },
      // 选中原已经选过的单元（灰色表示）
      selectUnit: function () {
        var _this = this;
        for (var i = 0; i < this.projectInfo.units.length; i++) {
          for (var j = 0; j < this.projects.unitdata.length; j++) {
            if (this.projects.unitdata[j].unitId === this.projectInfo.units[i].unit_id) {
              $('.last-unit span').eq(j).addClass('activeNot')
            }
          }
        }

      },
      // 选中原已经选过的车位及工位（灰色表示）
      selectUnitst: function () {
        var _this = this;
        //  车位
        for (var s = 0; s < this.projectInfo.stdata.length; s++) {
          var stAct = this.projectInfo.stdata[s].stop
          for (var j = 0; j < this.projects.stWpdata.length; j++) {
            for (var st = 0; st < stAct.length; st++) {
              if (this.projects.stWpdata[j].positionId == stAct[st].positionId) {
                $('.st-list span').eq(j).addClass('activeNot')
              }
            }
          }
        }

        //  工位
        for (var w = 0; w < this.projectInfo.wpdata.length; w++) {
          var wpAct = this.projectInfo.wpdata[w].stop;
          for (var j = 0; j < this.projects.stWpdata.length; j++) {
            for (var wp = 0; wp < wpAct.length; wp++) {
              if (this.projects.stWpdata[j].positionId == wpAct[wp].positionId) {
                $('.wp-list span').eq(j).addClass('activeNot')
              }
            }
          }
        }
      },
      // 关闭单元弹层
      closeUnitPop: function () {
        this.projectInfo.unitFlag = true;
        this.bounced.unitFlag = false;
        this.projects.unitdata = [];
        this.projects.floorsdata = [];
        this.projects.stWpdata = [];
        this.projects.buildingId = null;
        this.projects.unitId = null;
        this.projectInfo.units = JSON.parse(JSON.stringify(this.unitData));
        this.getname = {  buildname: null,  unitname: null,  floorname: null,  stop_acreage: null};
        $('.floor-select span, .unit-select span, .building-select span').text('请选择')
      },
      // 关闭车位弹层
      closeStPop: function () {
        this.bounced.stFlag = false;
        this.projectInfo.stFlag = true;
        this.projects.unitdata = [];
        this.projects.stWpdata = [];
        this.projects.floorsdata = [];
        this.projects.buildingId = null;
        this.projects.unitId = null;
        this.projectInfo.stdata = JSON.parse(JSON.stringify(this.stData));
        this.getname = {  buildname: null,  unitname: null,  floorname: null,  stop_acreage: null};
        $('.floor-select span, .unit-select span, .building-select span').text('请选择')
      },
      // 关闭工位弹层
      closeWpPop: function () {
        this.bounced.wpFlag = false;
        this.projectInfo.wpFlag = true;
        this.projects.unitdata = [];
        this.projects.floorsdata = [];
        this.projects.stWpdata = [];
        this.projects.buildingId = null;
        this.projects.unitId = null;
        this.projectInfo.wpdata = JSON.parse(JSON.stringify(this.wpData));
        this.getname = {  buildname: null,  unitname: null,  floorname: null,  stop_acreage: null};
        $('.floor-select span, .unit-select span, .building-select span').text('请选择')
      },
      // 确认添加单元、车位或者工位
      // @parame n == 1 单元    n == 2 工位    n == 3 车位
      confirmAddUnitStWp: function (n) {
        if (n == 1) {
          this.projectInfo.unitFlag = true;
          this.bounced.unitFlag = false;
          this.projects.unitdata = [];
          this.projects.floorsdata = [];
          this.projects.stWpdata = [];
          this.projects.buildingId = null;
          this.projects.unitId = null;
          this.getname = {  buildname: null,  unitname: null,  floorname: null,  stop_acreage: null};
          $('.floor-select span, .unit-select span, .building-select span').text('请选择')
          this.unitData = JSON.parse(JSON.stringify(this.projectInfo.units));
        }
        if (n == 2) {
          this.bounced.wpFlag = false;
          this.projectInfo.wpFlag = true;
          this.projects.unitdata = [];
          this.projects.floorsdata = [];
          this.projects.stWpdata = [];
          this.projects.buildingId = null;
          this.projects.unitId = null;
          this.getname = {  buildname: null,  unitname: null,  floorname: null,  stop_acreage: null};
          $('.floor-select span, .unit-select span, .building-select span').text('请选择')
          this.wpData = JSON.parse(JSON.stringify(this.projectInfo.wpdata));

          // 计算工位数
          var wpNum = 0;
          this.wpData.map(function(_item){
              wpNum += _item.stop.length;
          })
          this.wpNum = wpNum;
        }
        if (n == 3) {
          this.bounced.stFlag = false;
          this.projectInfo.stFlag = true;
          this.projects.unitdata = [];
          this.projects.stWpdata = [];
          this.projects.floorsdata = [];
          this.projects.buildingId = null;
          this.projects.unitId = null;
          this.getname = {  buildname: null,  unitname: null,  floorname: null,  stop_acreage: null};
          $('.floor-select span, .unit-select span, .building-select span').text('请选择')
          this.stData = JSON.parse(JSON.stringify(this.projectInfo.stdata));
          // 计算车位数
          var stNum = 0;
          this.stData.map(function(_item){
              stNum += _item.stop.length;
          })
          this.stNum = stNum;
        }
      },
      //删除单元
      deleteUnit: function (i) {
        this.unitData.splice(i, 1)
        this.projectInfo.units = this.unitData;
      },
      //删除车位
      deleteSt: function (i, n) {
        this.stData[i].stop.splice(n, 1)
        if (!this.stData[i].stop.length) {
          this.stData.splice(i, 1)
        }
        this.projectInfo.stdata = JSON.parse(JSON.stringify(this.stData));
      },
      //删除工位
      deleteWp: function (i, n) {
        this.wpData[i].stop.splice(n, 1)
        if (!this.wpData[i].stop.length) {
          this.wpData.splice(i, 1)
        }
        this.projectInfo.wpdata = JSON.parse(JSON.stringify(this.wpData));
      },
      //获取车位工位单元
      getStWpUnit: function (id, name, fname) {
        var self = this;
        this.projects.floorId = id;
        this.getname.floorname = name;
        this.getname.fname = fname;
        this.projects.unitdata = [];
        this.projects.stWpdata = [];
        $('.unit-select span').text('请选择')

        hlhttp.get({
          url: 'floor/units',
          data: {
            project_id: self.projects.id,  //项目ID
            floor_id: id,  //楼层ID
            building_id: self.projects.buildingId
          },
          success: function (res) {
            self.projects.unitdata = res;
          }
        })
      },
      //获取车位工位
      getswDt: function (id, name) {
        var _this = this;
        this.projects.unitId = id;
        this.getname.unitname = name;
        $('.st-unit span').removeClass('active')
        hlhttp.get({
          url: 'units/positions',
          data: {unit_id: id},
          success: function (res) {
            _this.projects.stWpdata = res;
            setTimeout(function() {
              _this.selectUnitst()
            }, 200);
          }
        })
      },
      //验证
      zlfname: function () {
        if (!this.zlfData.zlf_name) {
          hl.toast('请填写客户名称')
        }
      },
      validateIDcode:function(code){
        if(code === 'C'){
          if(!this.zlfData.idcode) hl.toast('请填写纳税人识别号')
          return false;
        }
        if(code === 'P'){
          if(!this.zlfData.idcode) hl.toast('请填写身份证号')
          return false;
        }
      },
      validateEmail:function(){
        var email = this.zlfData.email
        if(!email) return false;
        if(email){
          if(!hlValidateUtil.validateEmail(email)){
            hl.toast('邮箱格式不正确')
            this.zlfData.email = null;
            return false;
          }
        }
      },
      zlfperson: function () {
        if (!this.zlfData.zlf_sperson) {
          hl.toast('请填写联系人')
          return false;
        }
      },
      zlfphone: function () {
        if (!this.zlfData.zlf_sphone) {
          hl.toast('请填写联系电话')
          return false;
        }
        if (this.zlfData.zlf_sphone.length < 8) {
          hl.toast('联系电话填写错误')
          this.zlfData.zlf_sphone = null;
          return false;
        }
      },
      zlfaddress: function () {
        if (!this.zlfData.zlf_address) {
          hl.toast('请填写通讯地址')
          return false;
        }
      },

      validateNextStep:function(){
        if (!this.zlfData.zlf_name){
          hl.toast('请填写客户名称')
          return false;
        }
        if (!this.industryTypes.selected.name){
          hl.toast('请选择所属行业')
          return false;
        }
        if (!this.zlfData.idcode) {
          if(this.zlfType.active === 'C'){
            hl.toast('请填写纳税人识别号')
            return false;
          }
          if(this.zlfType.active === 'P'){
            hl.toast('请填写身份证号')
            return false;
          }
        }
        if (!this.zlfData.zlf_sperson){
          hl.toast('请填写联系人')
          return false;
        }
        if (!this.zlfData.zlf_sphone){
          hl.toast('请填写联系电话')
          return false;
        }
        if (!this.zlfData.zlf_address){
          hl.toast('请填写通讯地址')
          return false;
        }
        if (!this.projectInfo.units.length && !this.projectInfo.wpdata.length && !this.projectInfo.stdata.length){
          hl.toast('请选择项目')
          return false;
        }
        return true
      },
      //下一步
      nextData: function () {
        var self = this
        if(self.validateNextStep()){
          // 验证招商客户是否是自己和组员下的客户
          window.sessionStorage.setItem('EDIT_CONTRACT_FIRST_STEP', JSON.stringify(self.$data))
          hlhttp.put({
            url:'check_my_customer',
            data:{'customer_name':self.zlfData.zlf_name},
            success:function(data){
              if(data.myself === 'N'){hl.toast('客户已存在')}
              if(data.myself === 'Y'){
                self.projectInfo.units = JSON.parse(JSON.stringify(self.unitData))
                window.sessionStorage.setItem('EDIT_CONTRACT_FIRST_STEP', JSON.stringify(self.$data))
                if (hlUrlUtil.GetQueryString('relet') == 'true') {
                  hlUrlUtil.goTo('contractEdit2',{id:self.contractId, relet: true, ddtab:true})
                  return
                }
                hlUrlUtil.goTo('contractEdit2',{id:self.contractId, ddtab:true})
              }
            }
          })
        }
      }
    },
    beforeMount: function () {
      // 由上一步返回则从本地获取数据，否则直接获取合同详情数据
      if (window.sessionStorage.getItem('EDIT_CONTRACT_FIRST_STEP')) {
        var data = JSON.parse(window.sessionStorage.getItem('EDIT_CONTRACT_FIRST_STEP'));
        this.zlfData = data.zlfData
        this.bounced = data.bounced;
        this.contactType = data.contactType;
        this.industryTypes = data.industryTypes;
        this.projectInfo = data.projectInfo;
        this.unitData = data.unitData;
        this.wpData = data.wpData;
        this.stData = data.stData;
        this.stNum = data.stNum;
        this.wpNum = data.wpNum;
        this.projects = data.projects;
        this.zlfType = data.zlfType;
        this.intent = data.intent;
        this.channelType = data.channelType
        this.channelName = data.channelName
      }else{
        this.getContractDetail()
      }
    },
    mounted: function () {
      this.getIndustryTypes()
      this.getProjects()
      this.getChannelType()
      hlSelectUi()
    }
  })
} ())
